using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Web;

using PickGold.Charting.Utilities;
using PickGold.Charting.Design;

namespace PickGold.Charting
{
	[SRDescription("DescriptionAttributeTextAnnotation_TextAnnotation"), AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
	public class TextAnnotation : Annotation
	{
		private bool _isMultiline;
		private string _text = "";
		internal SizeF contentSize = SizeF.Empty;
		internal bool isEllipse;

		internal RectangleF DrawText(ChartGraphics graphics, RectangleF textPosition, bool noSpacingForCenteredText, bool getTextPosition)
		{
			RectangleF empty = RectangleF.Empty;
			bool annotationRelative = false;
			RectangleF textSpacing = this.GetTextSpacing(out annotationRelative);
			float num = 1f;
			float num2 = 1f;
			if (annotationRelative)
			{
				if (textPosition.Width > 25f)
				{
					num = textPosition.Width / 50f;
					num = Math.Max(1f, num);
				}
				if (textPosition.Height > 25f)
				{
					num2 = textPosition.Height / 50f;
					num2 = Math.Max(1f, num2);
				}
			}
			RectangleF rect = new RectangleF(textPosition.Location, textPosition.Size);
			rect.Width -= (textSpacing.Width + textSpacing.X) * num;
			rect.X += textSpacing.X * num;
			rect.Height -= (textSpacing.Height + textSpacing.Y) * num2;
			rect.Y += textSpacing.Y * num2;
			string titleText = base.ReplaceKeywords(this.Text.Replace(@"\n", "\n"));
			if (noSpacingForCenteredText && (titleText.IndexOf('\n') == -1))
			{
				if (((this.Alignment == ContentAlignment.MiddleCenter) || (this.Alignment == ContentAlignment.MiddleLeft)) || (this.Alignment == ContentAlignment.MiddleRight))
				{
					rect.Y = textPosition.Y;
					rect.Height = textPosition.Height;
					rect.Height -= (textSpacing.Height / 2f) + (textSpacing.Y / 2f);
					rect.Y += textSpacing.Y / 2f;
				}
				if (((this.Alignment == ContentAlignment.BottomCenter) || (this.Alignment == ContentAlignment.MiddleCenter)) || (this.Alignment == ContentAlignment.TopCenter))
				{
					rect.X = textPosition.X;
					rect.Width = textPosition.Width;
					rect.Width -= (textSpacing.Width / 2f) + (textSpacing.X / 2f);
					rect.X += textSpacing.X / 2f;
				}
			}
			using (new SolidBrush(this.ForeColor))
			{
				using (StringFormat format = new StringFormat(StringFormat.GenericTypographic))
				{
					format.FormatFlags ^= StringFormatFlags.LineLimit;
					format.Trimming = StringTrimming.EllipsisCharacter;
					if (((this.Alignment == ContentAlignment.BottomRight) || (this.Alignment == ContentAlignment.MiddleRight)) || (this.Alignment == ContentAlignment.TopRight))
					{
						format.Alignment = StringAlignment.Far;
					}
					if (((this.Alignment == ContentAlignment.BottomCenter) || (this.Alignment == ContentAlignment.MiddleCenter)) || (this.Alignment == ContentAlignment.TopCenter))
					{
						format.Alignment = StringAlignment.Center;
					}
					if (((this.Alignment == ContentAlignment.BottomCenter) || (this.Alignment == ContentAlignment.BottomLeft)) || (this.Alignment == ContentAlignment.BottomRight))
					{
						format.LineAlignment = StringAlignment.Far;
					}
					if (((this.Alignment == ContentAlignment.MiddleCenter) || (this.Alignment == ContentAlignment.MiddleLeft)) || (this.Alignment == ContentAlignment.MiddleRight))
					{
						format.LineAlignment = StringAlignment.Center;
					}
					Color baseColor = ChartGraphics.GetGradientColor(this.ForeColor, Color.Black, 0.8);
					int shadowOffset = 1;
					TextStyle textStyle = this.TextStyle;
					if ((textStyle == TextStyle.Shadow) && (this.ShadowOffset != 0))
					{
						baseColor = this.ShadowColor;
						shadowOffset = this.ShadowOffset;
					}
					if (textStyle == TextStyle.Shadow)
					{
						baseColor = (baseColor.A != 0xff) ? baseColor : Color.FromArgb(baseColor.A / 2, baseColor);
					}
					if (getTextPosition)
					{
						SizeF size = graphics.MeasureStringRel(base.ReplaceKeywords(this._text.Replace(@"\n", "\n")), this.Font, rect.Size, format);
						empty = new RectangleF(rect.Location, size);
						if (((this.Alignment == ContentAlignment.BottomRight) || (this.Alignment == ContentAlignment.MiddleRight)) || (this.Alignment == ContentAlignment.TopRight))
						{
							empty.X += rect.Width - size.Width;
						}
						if (((this.Alignment == ContentAlignment.BottomCenter) || (this.Alignment == ContentAlignment.MiddleCenter)) || (this.Alignment == ContentAlignment.TopCenter))
						{
							empty.X += (rect.Width - size.Width) / 2f;
						}
						if (((this.Alignment == ContentAlignment.BottomCenter) || (this.Alignment == ContentAlignment.BottomLeft)) || (this.Alignment == ContentAlignment.BottomRight))
						{
							empty.Y += rect.Height - size.Height;
						}
						if (((this.Alignment == ContentAlignment.MiddleCenter) || (this.Alignment == ContentAlignment.MiddleLeft)) || (this.Alignment == ContentAlignment.MiddleRight))
						{
							empty.Y += (rect.Height - size.Height) / 2f;
						}
						empty.Intersect(rect);
					}
					RectangleF absoluteRectangle = graphics.GetAbsoluteRectangle(rect);
					Title.DrawStringWithStyle(graphics, titleText, this.TextStyle, this.Font, absoluteRectangle, this.ForeColor, baseColor, shadowOffset, format, TextOrientation.Auto);
				}
			}
			return empty;
		}

		internal override RectangleF GetContentPosition()
		{
			if (!this.contentSize.IsEmpty)
			{
				return new RectangleF(float.NaN, float.NaN, this.contentSize.Width, this.contentSize.Height);
			}
			Graphics graphics = null;
			Image image = null;
			ChartGraphics graphics2 = null;
			if ((base.GetGraphics() == null) && (base.Common != null))
			{
				image = new Bitmap(base.Common.ChartPicture.Width, base.Common.ChartPicture.Height);
				graphics = Graphics.FromImage(image);
				graphics2 = new ChartGraphics(base.Common);
				graphics2.Graphics = graphics;
				graphics2.SetPictureSize(base.Common.ChartPicture.Width, base.Common.ChartPicture.Height);
				base.Common.graph = graphics2;
			}
			RectangleF empty = RectangleF.Empty;
			if ((base.GetGraphics() != null) && (this.Text.Trim().Length > 0))
			{
				this.contentSize = base.GetGraphics().MeasureString("W" + base.ReplaceKeywords(this.Text.Replace(@"\n", "\n")), this.Font, new SizeF(2000f, 2000f), StringFormat.GenericTypographic);
				this.contentSize.Height *= 1.04f;
				this.contentSize = base.GetGraphics().GetRelativeSize(this.contentSize);
				bool annotationRelative = false;
				RectangleF textSpacing = this.GetTextSpacing(out annotationRelative);
				float num = 1f;
				float num2 = 1f;
				if (annotationRelative)
				{
					if (this.contentSize.Width > 25f)
					{
						num = this.contentSize.Width / 25f;
						num = Math.Max(1f, num);
					}
					if (this.contentSize.Height > 25f)
					{
						num2 = this.contentSize.Height / 25f;
						num2 = Math.Max(1f, num2);
					}
				}
				this.contentSize.Width += (textSpacing.X + textSpacing.Width) * num;
				this.contentSize.Height += (textSpacing.Y + textSpacing.Height) * num2;
				empty = new RectangleF(float.NaN, float.NaN, this.contentSize.Width, this.contentSize.Height);
			}
			if (graphics2 != null)
			{
				graphics2.Dispose();
				graphics.Dispose();
				image.Dispose();
				base.Common.graph = null;
			}
			return empty;
		}

		internal virtual RectangleF GetTextSpacing(out bool annotationRelative)
		{
			annotationRelative = false;
			RectangleF rectangle = new RectangleF(3f, 3f, 3f, 3f);
			if (base.GetGraphics() != null)
			{
				rectangle = base.GetGraphics().GetRelativeRectangle(rectangle);
			}
			return rectangle;
		}

		internal override void Paint(Chart chart, ChartGraphics graphics)
		{
			PointF empty = PointF.Empty;
			PointF anchorLocation = PointF.Empty;
			SizeF size = SizeF.Empty;
			this.GetRelativePosition(out empty, out size, out anchorLocation);
			PointF tf3 = new PointF(empty.X + size.Width, empty.Y + size.Height);
			RectangleF rect = new RectangleF(empty, new SizeF(tf3.X - empty.X, tf3.Y - empty.Y));
			RectangleF textPosition = new RectangleF(rect.Location, rect.Size);
			if (textPosition.Width < 0f)
			{
				textPosition.X = textPosition.Right;
				textPosition.Width = -textPosition.Width;
			}
			if (textPosition.Height < 0f)
			{
				textPosition.Y = textPosition.Bottom;
				textPosition.Height = -textPosition.Height;
			}
			if ((textPosition.IsEmpty || float.IsNaN(textPosition.X)) || ((float.IsNaN(textPosition.Y) || float.IsNaN(textPosition.Right)) || float.IsNaN(textPosition.Bottom)))
			{
				return;
			}
			if (base.Common.ProcessModePaint)
			{
				this.DrawText(graphics, textPosition, false, false);
			}
			if (base.Common.ProcessModeRegions)
			{
				if (this.isEllipse)
				{
					using (GraphicsPath path = new GraphicsPath())
					{
						path.AddEllipse(textPosition);
						base.Common.HotRegionsList.AddHotRegion(graphics, path, true, base.ReplaceKeywords(this.ToolTip), base.ReplaceKeywords(this.Url), base.ReplaceKeywords(this.MapAreaAttributes), base.ReplaceKeywords(base.PostBackValue), this, ChartElementType.Annotation);
						goto Label_01FB;
					}
				}
				base.Common.HotRegionsList.AddHotRegion(textPosition, base.ReplaceKeywords(this.ToolTip), base.ReplaceKeywords(this.Url), base.ReplaceKeywords(this.MapAreaAttributes), base.ReplaceKeywords(base.PostBackValue), this, ChartElementType.Annotation, string.Empty);
			}
		Label_01FB:
			this.PaintSelectionHandles(graphics, rect, null);
		}

		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), SRCategory("CategoryAttributeMisc"), Browsable(false), EditorBrowsable(EditorBrowsableState.Never), SerializationVisibility(SerializationVisibility.Hidden), SRDescription("DescriptionAttributeTextAnnotation_AnnotationType"), Bindable(true)]
		public override string AnnotationType
		{
			get
			{
				return "Text";
			}
		}

		[SRCategory("CategoryAttributeAppearance"), Editor(typeof(ChartColorEditor), typeof(UITypeEditor)), Browsable(false), DefaultValue(typeof(Color), ""), NotifyParentProperty(true), TypeConverter(typeof(ColorConverter))]
		public override Color BackColor
		{
			get
			{
				return base.BackColor;
			}
			set
			{
				base.BackColor = value;
			}
		}

		[NotifyParentProperty(true), Browsable(false), DefaultValue(0), SRCategory("CategoryAttributeAppearance"), Editor(typeof(GradientEditor), typeof(UITypeEditor))]
		public override GradientStyle BackGradientStyle
		{
			get
			{
				return base.BackGradientStyle;
			}
			set
			{
				base.BackGradientStyle = value;
			}
		}

		[DefaultValue(0), Browsable(false), SRCategory("CategoryAttributeAppearance"), NotifyParentProperty(true), Editor(typeof(HatchStyleEditor), typeof(UITypeEditor))]
		public override ChartHatchStyle BackHatchStyle
		{
			get
			{
				return base.BackHatchStyle;
			}
			set
			{
				base.BackHatchStyle = value;
			}
		}

		[SRCategory("CategoryAttributeAppearance"), Browsable(false), DefaultValue(typeof(Color), ""), NotifyParentProperty(true), TypeConverter(typeof(ColorConverter)), Editor(typeof(ChartColorEditor), typeof(UITypeEditor))]
		public override Color BackSecondaryColor
		{
			get
			{
				return base.BackSecondaryColor;
			}
			set
			{
				base.BackSecondaryColor = value;
			}
		}

		[SRDescription("DescriptionAttributeTextFont4"), SRCategory("CategoryAttributeAppearance"), DefaultValue(typeof(System.Drawing.Font), "Microsoft Sans Serif, 8pt")]
		public override System.Drawing.Font Font
		{
			get
			{
				return base.Font;
			}
			set
			{
				base.Font = value;
				this.contentSize = SizeF.Empty;
			}
		}

		[DefaultValue(false), SRDescription("DescriptionAttributeMultiline"), SRCategory("CategoryAttributeAppearance")]
		public virtual bool IsMultiline
		{
			get
			{
				return this._isMultiline;
			}
			set
			{
				this._isMultiline = value;
				this.Invalidate();
			}
		}

		[Editor(typeof(ChartColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(ColorConverter)), SRCategory("CategoryAttributeAppearance"), Browsable(false), DefaultValue(typeof(Color), "Black")]
		public override Color LineColor
		{
			get
			{
				return base.LineColor;
			}
			set
			{
				base.LineColor = value;
			}
		}

		[Browsable(false), SRCategory("CategoryAttributeAppearance"), DefaultValue(5)]
		public override ChartDashStyle LineDashStyle
		{
			get
			{
				return base.LineDashStyle;
			}
			set
			{
				base.LineDashStyle = value;
			}
		}

		[Browsable(false), SRDescription("DescriptionAttributeLineWidth"), SRCategory("CategoryAttributeAppearance"), DefaultValue(1)]
		public override int LineWidth
		{
			get
			{
				return base.LineWidth;
			}
			set
			{
				base.LineWidth = value;
			}
		}

		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), DefaultValue(1), ParenthesizePropertyName(true), Browsable(false), EditorBrowsable(EditorBrowsableState.Never), SerializationVisibility(SerializationVisibility.Hidden), SRDescription("DescriptionAttributeSelectionPointsStyle"), SRCategory("CategoryAttributeAppearance")]
		internal override SelectionPointsStyle SelectionPointsStyle
		{
			get
			{
				return SelectionPointsStyle.Rectangle;
			}
		}

		[SRDescription("DescriptionAttributeText"), SRCategory("CategoryAttributeAppearance"), DefaultValue("")]
		public virtual string Text
		{
			get
			{
				return this._text;
			}
			set
			{
				this._text = value;
				this.Invalidate();
				this.contentSize = SizeF.Empty;
			}
		}
	}
}

